package org.rooster.manager;

import org.rooster.dbmanager.repository.RoosterRepository;
import org.rooster.schedule.ResourceRequirement;

import java.util.Iterator;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: manorama
 * Date: 6/3/12
 * Time: 6:49 PM
 * To change this template use File | Settings | File Templates.
 */
public class TaskManager {
    
    public static int NO_OF_SECTIONS = 5;
    public static int NO_OF_TIME_SLOTS = 8;

    public static int NO_OF_SLOTS_FOR_LUNCH = 2;
    public static int LUNCH_DURATION_IN_NO_OF_SLOTS = 1;
    public static int lunchSlotStart = 4;
    public static int lunchSlotEnd = 5;
    
    private static int[][][] requiredEmployees  = new int[NO_OF_SECTIONS][NO_OF_TIME_SLOTS][ResourceTypeInterface.NO_OF_RESOURCE_TYPES];

//    todo following comments to be removed

//    static {
//        requiredEmployees[0][0][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 2;
//        requiredEmployees[0][1][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[0][2][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[0][3][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[0][4][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//        requiredEmployees[0][5][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//        requiredEmployees[0][6][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[0][7][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//
//            requiredEmployees[0][0][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[0][1][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[0][2][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[0][3][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[0][4][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[0][5][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 0;
//            requiredEmployees[0][6][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[0][7][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//
//        requiredEmployees[1][0][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[1][1][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[1][2][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[1][3][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[1][4][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//        requiredEmployees[1][5][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//        requiredEmployees[1][6][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[1][7][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//
//            requiredEmployees[1][0][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[1][1][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[1][2][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[1][3][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[1][4][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[1][5][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[1][6][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[1][7][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//
//
//        requiredEmployees[2][0][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 4;
//        requiredEmployees[2][1][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[2][2][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 4;
//        requiredEmployees[2][3][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[2][4][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 2;
//        requiredEmployees[2][5][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//        requiredEmployees[2][6][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[2][7][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//
//            requiredEmployees[2][0][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[2][1][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[2][2][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[2][3][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[2][4][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 0;
//            requiredEmployees[2][5][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[2][6][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[2][7][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//
//        requiredEmployees[3][0][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[3][1][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[3][2][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[3][3][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[3][4][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 0;
//        requiredEmployees[3][5][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//        requiredEmployees[3][6][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[3][7][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//
//            requiredEmployees[3][0][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[3][1][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[3][2][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[3][3][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[3][4][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 0;
//            requiredEmployees[3][5][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[3][6][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[3][7][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//
//        requiredEmployees[4][0][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 2;
//        requiredEmployees[4][1][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[4][2][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//        requiredEmployees[4][3][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 3;
//        requiredEmployees[4][4][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//        requiredEmployees[4][5][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 0;
//        requiredEmployees[4][6][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 2;
//        requiredEmployees[4][7][ResourceTypeInterface.EMPLOYEE_TYPE_VOLUME] = 1;
//
//            requiredEmployees[4][0][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[4][1][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[4][2][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[4][3][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[4][4][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 0;
//            requiredEmployees[4][5][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[4][6][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//            requiredEmployees[4][7][ResourceTypeInterface.EMPLOYEE_TYPE_NON_VOLUME] = 1;
//    }

    public TaskManager() {
        initializeTaskRequirements();
    }

    private void initializeTaskRequirements() {
        
        //todo retrieve the number of tasks, timeslots and resource types using the count SQl statements

        Set resourceRequirementsSet = RoosterRepository.getInstance().retrieveResourceRequirementsForSchedule();
        Iterator iterator = resourceRequirementsSet.iterator();
        ResourceRequirement resourceRequirement;
        while (iterator.hasNext()) {
            resourceRequirement = (ResourceRequirement) iterator.next();
            requiredEmployees[resourceRequirement.getTaskId()]
                    [resourceRequirement.getTimeSlotId()]
                    [resourceRequirement.getResourceTypeId()] = resourceRequirement.getNoOfResourcesRequired();
        }
    }

    /**
     *
     * @param sectionNo
     * @param slotNo
     * @return
     */
    public static int getRequirement(int sectionNo, int slotNo) {
        int requirement = 0;
        for(int i=0; i< ResourceTypeInterface.NO_OF_RESOURCE_TYPES; i++) {
            requirement += requiredEmployees[sectionNo][slotNo][i];
        }
        return requirement;
    }

    /**
     *
     * @param sectionNo
     * @param timeSlot
     * @param resourceType
     * @return
     */
    public static int getResourceTypeRequirement(int sectionNo, int timeSlot, int resourceType) {
        return requiredEmployees[sectionNo][timeSlot][resourceType];
    }

    /***
     * return the total number of resources required
     * @return
     */
    public static int getTotalRequirement() {
        int count = 0;
        for (int i=0; i<NO_OF_SECTIONS; i++) {
            for(int j=0; j<NO_OF_TIME_SLOTS; j++) {
                for(int k =0; k< ResourceTypeInterface.NO_OF_RESOURCE_TYPES; k++) {
                    count += requiredEmployees[i][j][k];
                }
            }
        }
        return count;
    }

    
    public static int getTimeSlotMaxRequirement(int timeSlot) {
        int max = 0, timeSlotRequirementForSection;
        for(int i=0; i<NO_OF_SECTIONS; i++) {
            timeSlotRequirementForSection = getRequirement(i, timeSlot);
            if(timeSlotRequirementForSection > max) {
                max = timeSlotRequirementForSection;
            }
        }
        return max;
    }
}
